import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;


public class monsterLoader {

	private static List<String> monsters;
	
	static monster loadedMonster;
	static String loadName;
	static int loadMaxHP;
	static int loadMaxMana;
	
	final static Charset ENCODING = StandardCharsets.UTF_8;
	
	public static ArrayList<monster> loadMonsterData() {
		try {
			monsters = readMonsterFile("monsters.txt");

		} catch (IOException e) {

			e.printStackTrace();
		}
		
		ArrayList<monster> monsterArray = new ArrayList<monster>();
		monsterArray = analyzeMonsterData(monsterArray);
		
		return monsterArray;
		
	}
	
	public static ArrayList<monster> analyzeMonsterData(ArrayList<monster> monsterArray) {
		int whichRound = 0;
		int whichSort = 0;
		
		String monsterName = "";
		String monsterMaxHp = "";
		String monsterMaxMana = "";
		
		boolean initialization = true;
		
		for (int i = 0; i < monsters.size(); i++) {
					
			boolean skipThis = false;
			boolean breakLoop = false;
			String valueRead = monsters.get(i);
			String firstChar = valueRead.substring(0,1);
			String secondChar = valueRead.substring(1,2);
			
			if (valueRead.equals("ENDOFLIST")){
				breakLoop = true;
			}
			
			// Above, there is a loop breaking mechanism, which gets triggered when "ENDOFLIST" is found.
			// Below, explanation: On monsters.txt syntax, "m0" begins the data values for the first monster. The check is for "m0" to "mInfinity".
			// Combination of these mechanism checks all of the monsters starting from the first monster "m0" and stops when "ENDOFLIST" is found.
			// Infinite loop caution: Infinite loop will happen when there is no ENDOFLIST in the monsters.txt file.
			
			if (firstChar.equals("m") && secondChar.equals(Integer.toString(whichRound)) || breakLoop){

				whichRound ++;
				whichSort = 0;
				skipThis = true;				
				if (!initialization) {
				
					monster analyzedMonster = new monster((whichRound-1), monsterName, monsterMaxHp, monsterMaxMana);
					monsterArray.add(analyzedMonster);
					
				}	
								
			} 
			
			if (breakLoop) {
				break;
			} else {			
				initialization = false;
			}
			
			if (!skipThis && whichSort == 0) {
				monsterName = valueRead;
				whichSort ++;
			} else if (whichSort == 1) {
				monsterMaxHp = valueRead;
				whichSort ++;
			} else if (whichSort == 2) {
				monsterMaxMana = valueRead;
				whichSort++;
			} 
		
		}
		
		return monsterArray;
	}
	
	
	public static void testMonsterData(ArrayList<monster> monsterArray) {
		System.out.print(monsterArray.get(0).getName() + ", the first monster of the loaded database. Size of monster database: " + monsterArray.size() + " monsters.");
	}
	
	static List<String> readMonsterFile(String aFileName) throws IOException {
		Path path = Paths.get(aFileName);
		return Files.readAllLines(path, ENCODING);
	}
	
}
